<?php

namespace App\Service\Ses;

use App\Common\Result;

use App\Http\Controllers\Controller;
use App\Models\Customer\CustomerCodeModel;
use App\Models\Customer\CustomerModel;
use Mail;

class SesService extends Controller
{
    /**
     * @funtion 发送邮件验证码
     * @param $to
     * @return bool|mixed
     */
    public  function sendEmail($to)
    {
        if($this->sendEmailLimit($to)){
           Result::msg('202','error','单日邮件发送超出限制');
        }

        $info = config('mail.message');

        $code = rand(10000,99999);
        $count = CustomerCodeModel::where('email',$to)
            ->count();
        if($count){
            CustomerCodeModel::where('email',$to)
                ->update([
                    'code' => $code
                ]);
        }else{
            CustomerCodeModel::insert([
                    'email' => $to,
                    'code' => $code
                ]);
        }

        $flag = Mail::raw($info.$code, function($message)use($to) {
            $message->to($to)->subject("小贩app");
        });

    }


    public function checkUser($email)
    {
        $result = CustomerModel::where('email',$email)->first();
        if(!$result){
            return true;
        }
//        if($result->is_active == 0){
//            Result::msg('204','error','已经注册了邮件,前往激活');
//        }else if($result->is_active == 1){
            Result::msg('205','error','该邮箱已经被注册，请登入');
//        }
    }
    /**
     * 单天发送邮件限制
     *
     * @param $to
     * @return bool
     */
    public function sendEmailLimit($to)
    {
        return false;
    }
}